﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;
using System.IO;

public partial class new_add : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        if (string.IsNullOrEmpty(Request["tid"]))
        {
            return;
        }
        int tid = int.Parse(Request["tid"]);
        ddlType.DataTextField = "Title";
        ddlType.DataValueField = "Id";
        ddlType.DataSource = dal.Topics2Title.Where(x => x.Tid == tid).OrderBy(x=>x.index);
        ddlType.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.News news = new CMSModel.News();
            news.author = txtAuthor.Text;
            news.ctime = DateTime.Now;
            news.content = txtContent.Text;
            news.keyword = txtKeyWords.Text;
            news.title = txtTitle.Text;
            news.Summary = txtSummary.Text;
            news.Stock = txtStock.Text;
            news.Source = txtSource.Text;
            news.ptime = DateTime.Now;
            news.IsEnable = true;

            //添加相关股票
            if (!string.IsNullOrEmpty(txtStock.Text))
            {
                string[] cc = txtStock.Text.Split(',', '，');
                foreach (var c in cc)
                {
                    NewsRelatedStocks s = new NewsRelatedStocks();
                    s.NewsId = news.id;
                    s.Stock = c;
                    try
                    {
                        s.StockName = CommonBll.DicStocks[s.Stock];
                    }
                    catch
                    {
                    }
                    dal.NewsRelatedStocks.AddObject(s);
                }
            }
            dal.News.AddObject(news);
            dal.SaveChanges();
            CMSModel.Topics2News tNews = new Topics2News();
            tNews.NewsId = news.id;
            tNews.CTime = DateTime.Now;
            tNews.TitleId = int.Parse(ddlType.SelectedValue);
            try
            {
                tNews.ImgUrl = ViewState["filename"].ToString();
            }
            catch
            {
            }
            dal.Topics2News.AddObject(tNews);
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='news.aspx?tid="+Request["tid"]+"'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "AddNews", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterClientScriptBlock(GetType(), "aa", "alert('添加失败,请重试');", true);
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string pathFolder = Server.MapPath("~/file/topics/" + Request["tid"] + "/");
        string fileName = FileUpload1.FileName;
        string houzui = "";
        if (fileName.IndexOf(".") > -1)
        {
            houzui = fileName.Substring(fileName.IndexOf("."));
        }
        string guid=Guid.NewGuid().ToString();
        ViewState["filename"] = "/topics/" + Request["tid"] + "/" + guid + houzui;
        string rName = pathFolder + guid + houzui;
        FileInfo file = new FileInfo(rName);
        if (!file.Directory.Exists)
        {
            file.Directory.Create();
        }
        FileUpload1.SaveAs(rName);
        Image1.ImageUrl = "~/file/topics/" + Request["tid"] + "/" + guid + houzui;
    }
}